// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-29 10:40
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetcodeRepeat;

import java.util.Arrays;

public class ChainsToStarts {
    /**
     * 链式前向星建图
     */
    final static int N = 100010, M = 2 * N;
    static int[] head = new int[N], edge = new int[M], to = new int[M];
    static int idx = 0;

    static void add(int a, int b) {
        edge[idx] = head[a];
        to[idx] = b;
        head[a] = idx++;
    }

    public static void constructGraph(int[][] edges) {
        for (int[] e : edges) {
            add(e[0], e[1]);
            add(e[1], e[0]);
        }
    }

    /*-----遍历图节点-----*/
    public static void searchGraph(int start) {
        for (int i = head[start]; i != -1; i = edge[i]) {
            System.out.print(start + "->" + to[i] + "->");
        }
    }

    /*-----功能测试-----*/
    public static void main(String[] args) {
        Arrays.fill(head, -1);
        Arrays.fill(edge, -1);
        Arrays.fill(to, -1);
        int[][] edges = {{0, 1}, {0, 2}, {0, 3}, {1, 2}};
        constructGraph(edges);
        searchGraph(3);
    }
}
